package com.jams.goodox.adapter.lfadapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jams.goodox.R;
import com.jams.goodox.activity.qa.BannerDetailActivity;
import com.jams.goodox.entity.HomeCourse;
import com.jams.goodox.utils.TimeUtils;
import com.jams.goodox.widget.FlyBanner;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by HP on 2018/5/19.
 */

public class HomeFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = HomeFragmentAdapter.class.getSimpleName();
    private static final int TYPE_001 = 0;//编辑框
    private static final int TYPE_002 = 1;//按钮
    private Context context;

    private List<HomeCourse> list;

    private OnItemClickListener onItemClickListener;


    public interface OnItemClickListener {
        void OnItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.onItemClickListener = listener;
    }

    public HomeFragmentAdapter(Context context, List<HomeCourse> list ) {
        this.context = context;
        this.list = list;
    }


    @Override
    public int getItemViewType(int position) {
        Log.d("TTTT","type = " + list.get(position).getType());
        return list.get(position).getType() ;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == 0) {
            View view = LayoutInflater.from(context).inflate(R.layout.homefragment_text, parent, false);
            TextHolder viewHolder = new TextHolder(view);
            return viewHolder;
        }

        if(viewType == 1){
            View v = LayoutInflater.from(context).inflate(R.layout.homefragment_course,   parent,false);
            CourseViewHolder th = new CourseViewHolder(v);
            return th;
        }

        if(viewType == 2){
            View v = LayoutInflater.from(context).inflate(R.layout.homefragment_paycourse,   parent,false);
            PayCourseViewHolder th = new PayCourseViewHolder(v);
            return th;
        }

        //header 轮播图
        if (viewType == 3){
            View v = LayoutInflater.from(context).inflate(R.layout.homefragment_banner,   parent,false);

            BannerHolder bannerHolder = new BannerHolder(v);
            return  bannerHolder;
        }

        return null;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        HomeCourse classRoomBean = list.get(position);

        if(holder instanceof BannerHolder){
            List images = new ArrayList();
            List<String> titles = new ArrayList();
            titles.add("1");
            titles.add("2");

            images.add(R.mipmap.banner01);
            images.add(R.mipmap.banner02);
            //设置图片加载器
            ((BannerHolder) holder).banner.setOnItemClickListener(new FlyBanner.OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    Intent intent = new Intent(context,BannerDetailActivity.class);
                    intent.putExtra("position",position);
                    context.startActivity(intent);

                }
            });

            //设置图片集合
            ((BannerHolder) holder).banner.setImages(images);

//            ((BannerHolder) holder).banner.setOnBannerListener(new OnBannerListener() {
//                @Override
//                public void OnBannerClick(int position) {
//                    Log.d("TTTT0","position = " + position);
//                    Intent intent = new Intent(context,BannerDetailActivity.class);
//                    intent.putExtra("position",position);
//                    context.startActivity(intent);
//                }
//            });
        }

        if(holder instanceof TextHolder){

            ((TextHolder) holder).title.setText(classRoomBean.getTitle());
        }

        if(holder instanceof CourseViewHolder){
            Log.d("TTTT1","classRoomBean = " + classRoomBean.getType());
            ((CourseViewHolder) holder).title.setText(classRoomBean.getTitle());
            ((CourseViewHolder) holder).nickName.setText(classRoomBean.getNickName());
            ((CourseViewHolder) holder).timeLength.setText(TimeUtils.getTime(classRoomBean.getTimeLength()));

            final int pos = position;
            ((CourseViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG,"-----点击课程-------");
                    onItemClickListener.OnItemClick(pos);
                }
            });
        }

        if(holder instanceof  PayCourseViewHolder){
            ((PayCourseViewHolder) holder).title.setText(classRoomBean.getDescr());
            ((PayCourseViewHolder) holder).nickName.setText(classRoomBean.getNickName());
            ((PayCourseViewHolder) holder).price.setText("¥" + classRoomBean.getPrice());

            ((PayCourseViewHolder) holder).imageView.setImageURI(Uri.parse(classRoomBean.getImgPath()));

//            Glide.with(context).load(classRoomBean.getImgPath())
//                    //.centerCrop()
//                    .dontAnimate()
//                    .transform(new GlideCircleTransform(context)).into(((PayCourseViewHolder) holder).imageView);

            final int pos = position;
            ((PayCourseViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG,"-----点击课程-------");
                    onItemClickListener.OnItemClick(pos);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return list.isEmpty()? 0 : list.size();
    }



    class BannerHolder extends RecyclerView.ViewHolder{
        public FlyBanner banner ;

        public BannerHolder(View itemView) {
            super(itemView);
            banner = (FlyBanner) itemView.findViewById(R.id.banner);
        }
    }


    //显示标题
    class TextHolder extends RecyclerView.ViewHolder{
        public TextView title;

        public TextHolder(View itemView) {
            super(itemView);
            title = (TextView)itemView.findViewById(R.id.title);
        }
    }

    //课程内容
    class CourseViewHolder extends RecyclerView.ViewHolder{
        public TextView title;
        public TextView nickName;
        public TextView timeLength;


        public CourseViewHolder(View itemView) {
            super(itemView);
            title = (TextView)itemView.findViewById(R.id.title);
            timeLength = (TextView)itemView.findViewById(R.id.timeLength);
            nickName = (TextView)itemView.findViewById(R.id.nickName);
        }
    }


    //课程内容
    class PayCourseViewHolder extends RecyclerView.ViewHolder{
        public TextView title;
        public TextView nickName;
        public TextView price;
        public SimpleDraweeView imageView;


        public PayCourseViewHolder(View itemView) {
            super(itemView);
            title = (TextView)itemView.findViewById(R.id.title);
            nickName = (TextView)itemView.findViewById(R.id.nickName);
            price = (TextView)itemView.findViewById(R.id.price);
            imageView = (SimpleDraweeView)itemView.findViewById(R.id.imageView);
        }
    }
}
